package cn.zfs.datasource.controller;


import cn.zfs.datasource.advice.GlobalException;
import cn.zfs.datasource.entity.model.DataSource;
import cn.zfs.datasource.entity.vo.DataSourceVO;
import cn.zfs.datasource.service.DataSourceSevice;
import cn.zfs.datasource.util.BaseResponse;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;

@RestController
public class DataSourceController{

    @Autowired
    private DataSourceSevice dataSourceSevice;

    @PostMapping("createDao")
    public BaseResponse createDao(HttpServletRequest request, HttpServletResponse response, DataSourceVO dataSourceVO) throws GlobalException, SQLException, IOException {
        return dataSourceSevice.createDao(request, response, dataSourceVO);
    }

    @PostMapping("createModel")
    public BaseResponse createModel(HttpServletRequest request, HttpServletResponse response, DataSourceVO dataSourceVO) throws SQLException, IOException {
        return dataSourceSevice.createModel(request, response, dataSourceVO);
    }

    @GetMapping("testConnection")
    public BaseResponse testConnection(HttpServletRequest request, DataSource dataSourceProperties) throws GlobalException {
        try {
            return dataSourceSevice.testConnection(request, dataSourceProperties);
        } catch (SQLException e) {
            throw new GlobalException(e.getMessage());
        }
    }

    @GetMapping("getTableNames")
    public BaseResponse getTableNames(HttpServletRequest request, int pageSize, int pageNum, String tableName) throws GlobalException {
        try {
            return dataSourceSevice.getTableNames(request, pageSize, pageNum, tableName);
        } catch (SQLException e) {
            throw new GlobalException(e.getMessage());
        }
    }

    @GetMapping("test")
    public String urlParamTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        ServletOutputStream outputStream = response.getOutputStream();
        try{
            JSch jsch=new JSch();
            Session session = jsch.getSession("root","211.159.174.169",22);
            session.setPassword("zfs199528");
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config); // 为Session对象设置properties

            session.connect(30000);

            Channel channel=session.openChannel("shell");
            channel.setInputStream(new ByteArrayInputStream("ll \n".getBytes()));
            channel.setOutputStream(System.out);
            System.out.println("--------------------------");
            channel.setOutputStream(outputStream);
            System.out.println("=====================");
            channel.connect();
        }
        catch(Exception e){
            System.out.println(e);
        }
        return "456";
    }
}
